class ListNode{
    public int val;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}


public class MyLinkedList {
    public ListNode head;
    public void creatList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        this.head = listNode1;
    }
    public ListNode deleteDuplication(){
        ListNode cur = head;
        ListNode newHead = new ListNode(-1);
        ListNode tmp = this.head;
        while(cur != null) {
            if(cur.next != null && cur.val == cur.next.val) {
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                cur = cur.next;
            } else{
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        tmp.next = null;
        return newHead.next;
    }
    public ListNode partition(int x){
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = this.head;
        while(cur != null)
        {
            if(cur.val < x){
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                if(as == null)
                {
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null){
            return as;
        }
        be.next = as;
        if(ae != null){
            ae.next = null;
        }
        return bs;
    }
    public ListNode FindKthToTail(int k){
        if(k <= 0){
            System.out.println("k不合法");
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;
        while(k-1 > 0){
            if(fast.next != null){
                fast = fast.next;
                k--;
            }else{
                System.out.println("没有这个点");
                return null;
            }
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode middleNode(){
        ListNode slow = this.head;
        ListNode fast = this.head;
        ListNode cur = this.head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode reverseList() {
        ListNode cur = this.head;
        ListNode prev = null;
        ListNode newHead = null;
        ListNode curNext = null;
        while (cur != null) {
            curNext = cur.next;
            if (curNext == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;

        }
        return newHead;
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode resNode = new ListNode(0);
        ListNode cur = resNode;
        while(l1!=null&&l2!=null){
            if(l1.val<=l2.val){
                cur.next = l1;
                cur = cur.next;
                l1 = l1.next;
            }
            else{
                cur.next = l2;
                cur = cur.next;
                l2 = l2.next;
            }
        }
        if(l1!=null){
            cur.next = l1;
        }
        if(l2!=null){
            cur.next = l2;
        }
        return resNode.next;
    }
    public boolean HuiwenScture(){
        ListNode fast = this.head;
        ListNode slow = this.head;
        if(this.head == null)
            return true;
        if(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;

        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while(head != slow){
            if(head.val == slow.val){
                head = head.next;
                slow = slow.next;
            }else{
                return false;
            }if(head.next == slow){
                return true;
            }
        }
        return true;

    }

    public ListNode getIntersection(ListNode headA, ListNode headB){
        if(headA == null || headB == null){
            return null;
        }

        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        while(pl != null){
            lenA++;
        }
        pl = headA;
        while(ps != null){
            lenB++;
        }
        ps = headB;
        int len = lenA - lenB;
        if(len < 0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        while(len > 0){
            pl = pl.next;
            len--;
        }
        while(ps != pl){
            pl = pl.next;
            ps = ps.next;
        }
        if(pl == null || ps == null){
            return null;
        }
        return pl;
    }

    public ListNode detectCycle(ListNode head){
        ListNode fast = this.head;
        ListNode slow = this.head;
        if(head == null){
            return null;
        }
        while(fast != null){
            if(fast.next != null){
                fast = fast.next.next;
            }else{
                return null;
            }
            slow = slow.next;
            if(fast == slow){
                ListNode prev = head;
                while(prev != slow){
                    prev = prev.next;
                    slow = slow.next;
                }
                return prev;
            }
        }
        return null;
    }
}


















